package com.heima.kafka.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: wxw
 * @ClassName: ProducerController
 * @JdkVersion: JDK1.8
 * @Version: 1.0.0
 * @Since: 2025/2/21 14:24
 * @Description: 测试Kafka生产者
 */

@RestController
public class ProducerController {

    @Autowired
    private KafkaTemplate kafkaTemplate;


    @GetMapping("/send/{key}/{value}")
    public String sendMessage(@PathVariable("key") String key, @PathVariable("value") String value) {
        //kafkaTemplate.send("testTopic", key, value);  // 会触发按照key分区的策略
        kafkaTemplate.send("testTopic", 0, key, value); // 会触发指定分区策略

        return "ok";
    }


    @GetMapping("/send/{value}")
    public String sendMessage(@PathVariable("value") String value) {
        kafkaTemplate.send("testTopic", value); // 会触发轮询策略

        return "ok";
    }
}
